package com.parking.certified.controller;


import cn.hutool.core.bean.BeanUtil;
import com.parking.certified.dao.CertifiedAccountMapper;
import com.parking.certified.dto.CertifiedAccountClientDto;
import com.parking.certified.dto.CertifiedAccountDTO;
import com.parking.certified.dto.CertifiedDto;
import com.parking.certified.dto.SelectCertifiedAccountIdDTO;
import com.parking.certified.entity.CertifiedAccount;
import com.parking.certified.exception.CertifiedException;
import com.parking.certified.exception.CertifiedExceptionCode;
import com.parking.certified.form.AddCertifiedForm;
import com.parking.certified.form.CertifiedLoginForm;
import com.parking.certified.form.UpdateStateForm;
import com.parking.certified.service.CertifiedAccountService;
import com.parking.common.core.model.Result;
import com.parking.common.core.model.StateData;
import com.parking.common.core.util.JwtUtil;
import com.parking.common.web.aop.Permission;
import com.parking.common.web.util.RedisUtil;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 彭于晏
 * @since 2023年11月29日
 */
@RestController
@RequestMapping("/certifiedAccount")
@Api(tags = "物业账户信息")
public class CertifiedAccountController {
    @Resource
    private CertifiedAccountService certifiedAccountService;
    @Resource
    private CertifiedAccountMapper certifiedAccountMapper;
    @Resource
    private RedisUtil redisUtil;

    /***
     *
     * @param form
     * @return
     */
    @PostMapping("/addCertified.platform")
    @Permission(resources = "/addCertified.platform")
    @ApiOperation("添加物业")
    public Result add(@RequestBody AddCertifiedForm form,@RequestHeader("authorization") String authorization)throws Exception{
        String refreshToken = redisUtil.get(authorization);
        Integer adminId = JwtUtil.getPlatformId(refreshToken);
        certifiedAccountService.addCertified(form,adminId);
        return Result.getResult().setMessage("添加物业账号成功").setCode(200);
    }

    /***
     *
     * @param form
     * @param response
     * @return
     */
    @ApiOperation("物业登录")
    @PostMapping("/login")
    public Result certifiedLogin(@RequestBody @Valid CertifiedLoginForm form, HttpServletResponse response){
        CertifiedAccountDTO accountDTO = certifiedAccountService.certifiedLogin(form, response);
        //将用户信息存进Token
        String refreshToken = JwtUtil.createCentifiedToken(3 * 24 * 60,"certifiedAccountId",accountDTO.getCertifiedAccountId());
        String accessToken = JwtUtil.createCentifiedToken(30,"certifiedAccountId",accountDTO.getCertifiedAccountId());
        response.setHeader("authorization", accessToken);
        System.out.println("参数Token"+accessToken);
        redisUtil.set(accessToken,refreshToken,3*24*60*60L);
        response.setHeader("Access-Control-Expose-Headers", "authorization");
        return Result.getResult(CertifiedAccountDTO.class).setMessage("恭喜你,登录成功").setCode(200).setData(accountDTO);
    }

    @GetMapping("/loginOut")
    @ApiOperation("物业退出登录")
    public Result loginOut(@RequestHeader("authorization")String authorization)throws Exception{
        Claims claims = JwtUtil.parseCertifiedToken(authorization);
        Integer certifiedAccountId = (Integer) claims.get("certifiedAccountId");
        certifiedAccountService.loginOut(certifiedAccountId);
        return Result.getResult().setMessage("退出登录成功").setCode(200);
    }
    /***
     *平台查询物业账号
     * @param certifiedAccountId
     * @return
     */
    @GetMapping("/selectCertifiedAccount.platform")
    @Permission(resources = "/selectCertifiedAccount.platform")
    @ApiOperation("平台查询物业账号")
    public Result selectCertifiedAccount(Integer certifiedAccountId,@RequestHeader("authorization")String authorization)throws Exception{
        String refreshToken = redisUtil.get(authorization);
        Integer platformId = JwtUtil.getPlatformId(refreshToken);
        List<CertifiedAccountDTO> dtos = certifiedAccountService.selectCertifiedAccount(certifiedAccountId,platformId);
        return Result.getResult().setMessage("查询成功").setCode(200).setData(dtos);
    }

    /***
     *平台修改物业账号状态
     * @param
     * @return
     * @throws Exception
     */
    @GetMapping("/enableCertifiedState.platform")
    @Permission(resources = "/enableCertifiedState.platform")
    @ApiOperation("平台启用物业账号")
    public Result enableCertifiedState(@RequestHeader("authorization")String authorization,Integer certifiedAccountId)throws Exception{
        Integer platformId = JwtUtil.getPlatformId(authorization);
        certifiedAccountService.enableCertifiedState(certifiedAccountId,platformId);
        return Result.getResult().setMessage("启用成功").setCode(200);
    }

    /***
     * 平台删除物业
     * @param authorization
     * @param certifiedAccountId
     * @return
     * @throws Exception
     */
    @GetMapping("/deleteCertifiedState.platform")
    @Permission(resources = "/deleteCertifiedState.platform")
    @ApiOperation("平台删除物业")
    public Result deleteCertifiedState(@RequestHeader("authorization")String authorization,Integer certifiedAccountId)throws Exception{
        Integer platformId = JwtUtil.getPlatformId(authorization);
        certifiedAccountService.deleteCertifiedState(certifiedAccountId,platformId);
        return Result.getResult().setMessage("删除成功").setCode(200);
    }

    /***
     * 平台禁用物业
     * @param authorization
     * @param certifiedAccountId
     * @return
     * @throws Exception
     */
    @GetMapping("/disableCertifiedState.platform")
    @Permission(resources = "/disableCertifiedState.platform")
    @ApiOperation("平台禁用物业")
    public Result disableCertifiedState(@RequestHeader("authorization")String authorization, Integer certifiedAccountId)throws Exception{
        String claims = redisUtil.get(authorization);
        Integer platformId = JwtUtil.getPlatformId(claims);
        certifiedAccountService.disableCertifiedState(certifiedAccountId,platformId);
        return Result.getResult().setMessage("禁用成功").setCode(200);
    }


    /**
     * 根据ID查询物业账号
     * 参数: 物业账号Id certifiedAccountId
     * 返回: 物业账号Id certifiedAccountId
     *      物业账号 certifiedAccountAccount
     * 方法: 1.判断certifiedAccountId是否存在对应的物业账号 若不存在则抛异常
     *      2.判断对应的物业账号的状态是否是启用的 若不是则抛异常
     *      3.将物业账号放入Dto返回
     * */
    @GetMapping("/selectCertifiedAccountById")
    @ApiOperation("根据ID查询物业(订单模块调用)")
    Result<CertifiedAccountClientDto> selectCertifiedAccountById(@RequestParam("certifiedAccountId") Integer certifiedAccountId){
        CertifiedAccountClientDto dto = certifiedAccountService.selectCertifiedAccountById(certifiedAccountId);
        return Result.getResult(CertifiedAccountClientDto.class).setData(dto);
    }
    /**
     * certifiedAccountCall：查询物业账号（车位模块调用）
     * @param certifiedAccountId:物业账号id
     * @return SelectCertifiedAccountIdDTO：物业账号信息参数对象
     */
    @GetMapping("/certifiedAccountCall")
    @ApiOperation("查询物业账号（车位模块调用）")
    Result<SelectCertifiedAccountIdDTO> certifiedAccountCall(@RequestParam("certifiedAccountId") Integer certifiedAccountId){
        if (certifiedAccountId==0||certifiedAccountId==null) {
            throw new CertifiedException(CertifiedExceptionCode.CERTIFIED_IS_NULL);
        }
        CertifiedAccount account = certifiedAccountMapper.selectById(certifiedAccountId);
        if (account==null){
            throw new CertifiedException(CertifiedExceptionCode.CERTIFIED_IS_NOT_CERTIFIED);
        }
        if (account.getCertifiedAccountState()!= StateData.CERTIFIED_STATE_ENABLE){
            throw new CertifiedException(CertifiedExceptionCode.CERTIFIED_IS_NOT_ENABLE);
        }
        SelectCertifiedAccountIdDTO dto = BeanUtil.toBean(account, SelectCertifiedAccountIdDTO.class);
        return Result.getResult().setMessage("调用成功").setData(dto).setCode(200);
    }

    /***
     * 查询物业信息
     * @param certifiedAccountId
     * @return CertifiedAccount
     *
     */
    @GetMapping("/getCertifiedById")
    @ApiOperation("查询物业信息（出租客模块调用）")
    private Result getCertifiedById(@RequestParam("certifiedAccountId")Integer certifiedAccountId){
        CertifiedAccount certified = certifiedAccountService.getById(certifiedAccountId);
        CertifiedDto certifiedDto = BeanUtil.toBean(certified, CertifiedDto.class);
        return Result.getResult(CertifiedDto.class).setCode(200).setMessage("调用成功").setData(certifiedDto);
    }




}

